﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TakeoutExpress.Support.Controllers
{
    public class AudienceController
    {
        private static Linq.TakeoutExpress2008DataContext context;
        public AudienceController()
        {
            context = new TakeoutExpress.Support.Linq.TakeoutExpress2008DataContext();
        }

        public IEnumerable<Linq.Audience> GetSiteAudiences(int siteId)
        {
            var audiences = (from a in context.Audiences
                             where a.SiteID == siteId
                                 select a);

            return audiences;
        }

        public Linq.Audience AddAudience(int siteId, string audienceName)
        {
            Linq.Audience audience = new TakeoutExpress.Support.Linq.Audience();
            audience.SiteID = siteId;
            audience.Name = audienceName;
            context.Audiences.InsertOnSubmit(audience);
            context.SubmitChanges();

            return audience;
        }

        public Linq.Audience GetAudience(int audienceId)
        {
            var audience = (from a in context.Audiences
                            where a.Id == audienceId
                            select a).Single();

            return audience;
        }

        public Linq.Audience UpdateAudience(Linq.Audience updatedAudience)
        {
            var audience = GetAudience(updatedAudience.Id);

            audience.Name = updatedAudience.Name;
            audience.Site = updatedAudience.Site;

            context.SubmitChanges();

            return audience;
        }

        public bool DeleteAudience(int audienceId)
        {
            var audience = GetAudience(audienceId);

            context.Audiences.DeleteOnSubmit(audience);
            context.SubmitChanges();

            return true;
        }

    }
}
